package com.wzg.pldroidplayer.recyclerviewdemo.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wzg.pldroidplayer.recyclerviewdemo.Main2Activity;
import com.wzg.pldroidplayer.recyclerviewdemo.R;
import com.wzg.pldroidplayer.recyclerviewdemo.Util.ToastUtil;
import com.wzg.pldroidplayer.recyclerviewdemo.VideoPlayerReady;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by WZG on 2018/4/12.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<String> mDatas;
    private List<Integer> mHeights;
    private Context mContext;

    public MyAdapter(List<String> mDatas) {
        this.mDatas = mDatas;
        mHeights = new ArrayList<>();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_recycler_view, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
/*int layoutPosition = holder.getLayoutPosition();*/

        // 获取数据
        String data = mDatas.get(position);
        // 将标签的高度设置成随机模拟瀑布流效果
        if (mHeights.size() <= position) {
            mHeights.add((int) (30 + Math.random() * 200));
        }
        ViewGroup.LayoutParams layoutParams = holder.mTvLabel.getLayoutParams();
        layoutParams.height = mHeights.get(position);
        WindowManager wm = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        // TODO: 2018/4/12 按比例设置item的宽度
        int width = wm.getDefaultDisplay().getWidth()/3;
        layoutParams.width = width;
        holder.mTvLabel.setLayoutParams(layoutParams);
        // 设置标签
        holder.mTvLabel.setText(data);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ToastUtil.showToast(mContext, "Item: " + position, Toast.LENGTH_SHORT);
                Intent intent = new Intent(mContext,Main2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putString("videoUrl",position+1+"");
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
//                ToastUtil.showToast(mContext, "你长按了: " + position+1, Toast.LENGTH_SHORT);
                mContext.startActivity(new Intent(mContext, VideoPlayerReady.class));
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView mTvLabel; // 标签
        private ImageView mTvImage;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTvLabel = itemView.findViewById(R.id.tv_label);
            mTvImage = itemView.findViewById(R.id.tv_img);
        }
    }
}
